package com.example.prolyric;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.prolyric.songmanager.ModePlaylist;
import com.example.prolyric.songmanager.Song;
import com.example.prolyric.songmanager.SongsManager;

public class AddPlaylist extends ListActivity {
	Button btnSave , btnCancel,btnBack;
	private InteractiveArrayAdapter iaa; 
	private SongsManager sm ;
	SongsManager pm ;
	private List<Song> songs; 
	private boolean status = false;
	private EditText ed; 
	private String playlistName; 
	private  AlertDialog.Builder inputDialog;
	private ArrayList<Song> selectedSong = new ArrayList<Song>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_playlist);
        
        btnSave = (Button)findViewById(R.id.btn_add_playlist_save);
        btnCancel = (Button)findViewById(R.id.btn_add_playlist_cancel);
        btnBack = (Button)findViewById(R.id.btn_add_playlist_back);
        
        sm= new SongsManager(this);
        pm = new SongsManager(this);
        
        songs = sm.getAllSong();
     // Create an array of Strings, that will be put to our ListActivity
       
         iaa = new  InteractiveArrayAdapter(this, getModePlaylist());
        //ArrayAdapter<ModePlaylist> adapter = new InteractiveArrayAdapter(this,  getModePlaylist());
        setListAdapter(iaa);
        
        btnSave.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				List<ModePlaylist> list = iaa.getList();
			
				for(int i = 0 ; i< list.size();i++)
				{
					if(list.get(i).isSelected())
					{
						selectedSong.add(songs.get(i));
					}
				}
				inputDialog.show();
				Toast.makeText(getBaseContext(), playlistName, Toast.LENGTH_LONG).show();
				//pm.savePlaylist(selectedSong, playlistName);
				/*for(int i = 0 ; i< selectedSong.size(); i++)
				{
					Log.d("cac bai duoc chon", selectedSong.get(i).getSong_display_name());
				}*/
				//pm.savePlaylist(playlist, playlistName)
			
				
				
			}
		});
        
        btnCancel.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				
				finish();
			}
		});
        
        btnBack.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				finish();
				
			}
		});
        
       
        inputDialog = new AlertDialog.Builder(this);
        inputDialog.setTitle("Enter playlist name !!!");
        ed = new  EditText(this);
        ed.setWidth(250);
        ed.setHeight(30);
        inputDialog.setView(ed);
        inputDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				playlistName = ed.getText().toString();
				StringBuffer strf = new StringBuffer(playlistName);
				strf.append(".ppl");
				pm.savePlaylist(selectedSong, strf.toString());
				AddPlaylist.this.finish();
				
				
			}
		});
        inputDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				
				
			}
		});
        
        
    }

   

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_add_playlist, menu);
        return true;
    }
    private List<ModePlaylist> getModePlaylist() {
        List<ModePlaylist> list = new ArrayList<ModePlaylist>();
        
        if(songs == null)
        	list = null;
        else{
        	
       
		       for(Song s : songs)
		       {
		    	   String song_display = s.getSong_display_name();
		    	   song_display = song_display.substring(0, song_display.length()-4);
		    	   ModePlaylist m = new ModePlaylist(song_display);
		    	   list.add(m);
		       }
        }
        return list;
      }

      private ModePlaylist get(String s) {
        return new ModePlaylist(s);
      }
}
